
use "Data\MFPS Child Health Paper Replication Data - Child Level.dta", clear

* Set Panel Variables
xtset caseid_n year

*Replace factor variable notation
tab cluster_num, gen(clust)
tab age_group_bl, gen(age_g_bl)
tab birth_order, gen(birth_o_dum)
tab total_birth_bl, gen(total_b_bl)
tab chi1m, gen(MOB)

/*
*create dummy variables for missing data
local cov cluster_num work_bl ever_use_bl curr_use_bl sex_age_bl age_group_bl edu_primary_bl birth_order religion_r_bl ethnicity_r_bl total_birth_bl child_sex wealthscore_bl child_age_months chi1m 

foreach i of local cov{ 
	gen `i'_missing = 0 
	replace `i'_missing = 1 if `i'==.
	replace `i' = 0 if `i'==.
}
*/

* Create global of covariates
global covariates "clust* work_bl ever_use_bl curr_use_bl sex_age_bl age_g_bl* edu_primary_bl birth_o_dum* religion_r_bl ethnicity_r_bl total_b_bl* child_sex"

*Create global of age covariates
mkspline age1 6 age2 12 age3 18 age4 24 age5 30 age6 = child_age_months

*Create extensive margins of intervention components
local wave w2 w3
local compo couns trans 
foreach j of local wave{
	foreach i of local compo{
		gen `j'_`i'_dum = `j'_`i'_count>0
	}
}


*Covariates that can only be used with the first child
*birthweight timetobreast wantedness 
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* %%%%% REGRESSIONS AND TABLES %%%%%
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/////////////////////////////////////
** %%% IV ESTIMATION %%% **
/////////////////////////////////////

local outcomes haz06 credi_z_score
global cov_group_1 "$agevars" 
global cov_group_2 "$agevars $covariates"


local i=1
foreach y of local outcomes{
	if `i' == 1{
		local name = "haz"
		local year = 2017
		local exposure w2_couns_count w2_couns_dum w2_trans_count w2_trans_dum
		global agevars "MOB* age1-age6"
	}
	if `i' == 2{
		local name = "credi"
		local year = 2018
		local exposure w3_couns_count w3_couns_dum w3_trans_count w3_trans_dum
		global agevars "age1-age6"
	}
	
	putexcel set "Results\IV\First Stage F_`name'.xlsx", replace
		putexcel B2 = ("Partial F-Stat")

	
	local j = 1
	foreach x of local exposure{
		forvalues z =1/2{
			eststo `name'_`j': ivreg2 `y' ${cov_group_`z'} (`x' = treatment) if year==`year' & index_dummy==1 & born_during_study==0, cluster(woman_id) ffirst
				
				cap matrix clear
				matrix define first = e(first)
			
					local cell = `j'+1
					putexcel A`cell' = ("`name'_`j''")
					putexcel B`cell' = (first[4,1])
	
			local j = `++j'
		}
	}
	local i = `++i'
	
	*** Tabulate main results
	esttab `name'_1 `name'_2 `name'_3 `name'_4 `name'_5 `name'_6 `name'_7 `name'_8 ///
	using "Results\IV\\`name' (IV).rtf", ///
	replace se ///
	scalar(N r2 F first)  mlabels("couns (intensive, unadjust)" "couns (intensive, adjust)" "couns (extensive, unadjust)" ///
	"couns (extensive, adjust)" "trans (intensive, unadjust)" "trans (intensive, adjust)" ///
	"trans (extensive, unadjust)" "trans (extensive, unadjust)") collabels(none) ///
	starlevels(* .1 ** .05 *** .01)     ///
	compress keep(`exposure' _cons) ///
	order(`exposure')	
	
}
